<script>
import { des_encrypt } from '@/static/js/crypto.js';
import Vue from 'vue';

export default {
	onLaunch: async function (option) {
		// 禁止横屏
		// #ifdef APP-PLUS
		plus.screen.lockOrientation('portrait-primary');
		plus.push.clear();
		// #endif
		this.globalData.invite_id = option.query.invite_id || null;
		if (!uni.getStorageSync('entryTime')) {
			uni.setStorageSync('entryTime', new Date().getTime());
		}
		this.globalData.token = uni.getStorageSync('token') || null;
		let info = uni.getStorageSync('stepInfo') || {};
		if (!this.globalData.token) {
			uni.setStorageSync('stepInfo', info);
		}
		// console.log('App Launch');
		// 动态渲染css变量值
		// document.getElementsByTagName('body')[0].style.setProperty('--primary', 'yellow');
	},
	onShow: async function () {
		let that = this;
		Vue.prototype.getBaikeLogobaseBg();
		that.globalData.provinceInfo = uni.getStorageSync('provinceLiscationInfo') || {};
	},
	onHide: function () {
		// console.log('App Hide');
	},
	methods: {
		// method 监听后触发的方法，istr监听字段
		watch(method, istr) {
			let obj = this.globalData;
			Object.defineProperty(obj, istr, {
				configurable: true,
				enumerable: true,
				set: function (value) {
					this._consumerGoodsStatus = value;
					method(value);
				},
				get: function (value) {
					return this._consumerGoodsStatus;
				}
			});
		},
		getClientId() {
			let that = this;
			uni.getPushClientId({
				success: async (res) => {
					let push_clientid = res.cid;
					let { code, data } = await that.$application_api.setPushClientId(
						{
							push_client_id: push_clientid
						},
						false
					);
					// console.log(code, data,'客户端推送标识:', push_clientid)
					// /student/set_push_client_id
				},
				fail(err) {
					console.log(err);
				}
			});
		},
		async appGetProvinceList() {
			let that = this;
			let { code, data } = await that.$application_api.getProvinceList({}, false);
			if (code != 0) return;
			that.globalData.provinceList = data || [];
		},
		// 获取考试年份列表接口
		async getYearList() {
			let that = this;
			let { code, data } = await that.$application_api.getTestYearList({}, false);
			if (code != 0) return false;
			that.globalData.yearList = data || [];
			return true;
		},
		// 下载静态文件
		async getStaticInfo(id) {
			let that = this;
			let staticIP = that.$domain.staticResources;
			const provinceInfo = getApp().globalData.provinceInfo || {};
			const selfInfo = uni.getStorageSync('selfInfo') || {};
			staticIP += `province_${id || provinceInfo.province_id || selfInfo.province_id || 1}.json`;
			uni.request({
				url: staticIP, // 替换为你要读取的线上文件的URL
				method: 'GET',
				success: function (res) {
					let info = res.data || {};
					that.globalData.staticInfo = info || {};
				},
				fail: function () {
					that.globalData.staticInfo = {};
				}
			});
		},
		getSysUserInfo() {
			const selfInfo = uni.getStorageSync('selfInfo') || {};
			if(!selfInfo.id) return;
			let allInfo = uni.getSystemInfoSync();
			let arr = ['SDKVersion', 'appLanguage', 'appName', 'devicePixelRatio', 'hostLanguage', 'language', 'safeArea', 'pixelRatio'];
			let info = {};
			for (const key in allInfo) {
				if (!arr.includes(key)) {
					info[key] = allInfo[key];
				}
			}
			let gzip = this.globalData.sysInfoBase64;
			if (this.globalData.token && !this.globalData.sysInfoBase64) {
				gzip = des_encrypt(JSON.stringify(info), this.globalData.token, selfInfo.id);
			}
			this.globalData.sysInfoBase64 = gzip;
			return gzip;
		},
		// 获取公共静态资源文件
		async getPublicStaticInfo() {
			let that = this;
			let allInfo = uni.getSystemInfoSync();
			// 当前的版本号+型号
			let platform = allInfo.platform?allInfo.platform.toLowerCase():'';
			let brand = allInfo.brand?allInfo.brand.toLowerCase():''; // 品牌
			let staticIP = that.$domain.staticResources + 'common.json';
			that.globalData.isOpenAppToWechat = false;
			uni.request({
				url: staticIP, // 替换为你要读取的线上文件的URL
				method: 'GET',
				success: function (res) {
					let info = res.data || {};
					that.globalData.commonStaticInfo = info || {};
					// #ifdef APP-PLUS
					plus.navigator.closeSplashscreen();
					plus.runtime.getProperty(plus.runtime.appid, (verRes) => {
						let appVersionCode = (verRes.version + '' + verRes.versionCode).split('.').join('');
						if(platform == 'ios'){
							brand = 'ios'
						}
						if (!that.globalData.commonStaticInfo[`close_${brand}_pay_version`]) {
							that.globalData.isOpenAppToWechat = true;
						} else {
							that.globalData.isOpenAppToWechat = !(that.globalData.commonStaticInfo[`close_${brand}_pay_version`] == appVersionCode);
						}
					});
					// #endif
					// #ifndef APP-PLUS
					that.globalData.isOpenAppToWechat = true;
					// #endif
				},
				fail: function () {
					// #ifdef APP-PLUS
					plus.navigator.closeSplashscreen();
					// #endif
					that.globalData.commonStaticInfo = {};
				}
			});
		}
	},
	globalData: {
		staticLink: 'https://h5.baikelive.com/jump_qr?id=103', // 默认h5链接
		token: '',
		provinceList: [],
		yearList: [],
		provinceInfo: {},
		locatinProvince: {}, // 位置定位信息
		invite_id: null, // 邀请人id
		sysInfoBase64: null,
		staticInfo: {}, // 省份静态资源
		commonStaticInfo: {}, // 公共静态数据
		isOpenAppToWechat: false, // 针对于系统判断当前版本是否审核中
	}
};
</script>

<style lang="scss">
/*每个页面公共css */
@import '@/uni_modules/uni-scss/index.scss';
// @import '@/static/scss/elfChart.scss';
/* #ifndef APP-PLUS-NVUE */
@import '@/plugin/iconfont/iconfont.css';
@import '@/static/scss/public.scss';

/* #endif */
@font-face {
	font-family: SourceHanSansCn;
	src: url('./static/SOURCEHANSANSCN.ttf');
	font-size: 32rpx;
	color: #121317;
}

.global-font {
	font-family: SourceHanSansCn;
}

/* #ifndef APP-PLUS-NVUE */
view,
text,
p,
div,
span,
i {
	font-family: SourceHanSansCn;
}

/deep/ uni-icons,
uni-text {
	display: inline-flex;
	align-items: center;
}

/* #endif */


</style>
